#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QLabel>
#include <QPropertyAnimation>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPushButton *pStartButton = ui->pushButton;
    pStartButton->setText(QString("开始动画"));

    QList<QLabel *> list;
    QStringList strList;
    strList << QString("一去丶二三里") << QString("青春不老，奋斗不止");

//    for (int i = 0; i < strList.count(); ++i)
    {
        QLabel *pLabel = ui->label;
        pLabel->setText(strList.at(0));
        pLabel->setAlignment(Qt::AlignCenter);
        pLabel->setStyleSheet("color: rgb(0, 160, 230);");
        list.append(pLabel);

        QLabel *pLabe2 = ui->label_2;
        pLabel->setText(strList.at(1));
        pLabel->setAlignment(Qt::AlignCenter);
        pLabel->setStyleSheet("color: rgb(0, 160, 230);");
        list.append(pLabe2);
    }

    // 动画一
    QPropertyAnimation *pAnimation1 = new QPropertyAnimation(list.at(0), "geometry");
    pAnimation1->setDuration(1000);
    pAnimation1->setStartValue(QRect(0, 0, 100, 30));
    pAnimation1->setEndValue(QRect(120, 130, 100, 30));
    pAnimation1->setEasingCurve(QEasingCurve::OutBounce);

    // 动画二
    QPropertyAnimation *pAnimation2 = new QPropertyAnimation(list.at(1), "geometry");
    pAnimation2->setDuration(1000);
    pAnimation2->setStartValue(QRect(120, 130, 120, 30));
    pAnimation2->setEndValue(QRect(170, 0, 120, 30));
    pAnimation2->setEasingCurve(QEasingCurve::OutInCirc);

    m_pGroup = new QParallelAnimationGroup(this);

    // 添加动画
    m_pGroup->addAnimation(pAnimation1);
    m_pGroup->addAnimation(pAnimation2);

    // 循环2次
    m_pGroup->setLoopCount(2);

    connect(pStartButton, SIGNAL(clicked(bool)), this, SLOT(startAnimation()));
}

// 开始动画
void MainWindow::startAnimation()
{
    m_pGroup->start();
}

MainWindow::~MainWindow()
{
    delete ui;
}
